package com.isczy.eduservice.controller;


import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isczy.eduservice.entity.EduTeacher;
import com.isczy.eduservice.entity.vo.TeacherQueryVo;
import com.isczy.eduservice.service.EduTeacherService;
import com.isczy.servicebase.exceptionHandler.MyException;
import com.isczy.utils.DataResult;
import com.isczy.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/********************************************
 * @Brief: 讲师 前端控制器
 * @Version: 1.0
 * @Date: 2020年12月25日 16:46:46
 * @Author: ChangZiYang
 * ******************************************
 */
@Api(description = "讲师 前端控制器")
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    @ApiOperation(value = "查询所有教师")
    @GetMapping("findAll")
    public DataResult findAll(){
        try {
            return DataResult.builder()
                    .data(eduTeacherService.list())
                    .successBuild();
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("查询所有教师失败")
                    .build();
        }
    }

    @ApiOperation(value = "通过id逻辑删除教师")
    @DeleteMapping("deleteById/{id}")
    public DataResult deleteById(@ApiParam(name = "id",value = "讲师id",required = true)@PathVariable String id){
        try {
            return DataResult.builder()
                    .data(eduTeacherService.removeById(id))
                    .successBuild();
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("通过id逻辑删除教师失败")
                    .params(id)
                    .build();
        }
    }

    @ApiOperation(value = "分页查询讲师")
    @GetMapping("pageTeacher/{current}/{limit}")
    public DataResult pageTeacher(@ApiParam(name = "current",value = "当前页码数",required = true)@PathVariable Long current,
                                  @ApiParam(name = "limit",value = "每页显示条数",required = true)@PathVariable Long limit){

        try {
            //调用方法实现，底层将查询出的数据封装到pageTeacher对象中
            Page<EduTeacher> pageTeacher = eduTeacherService.page(new Page<>(current, limit), null);
            return DataResult.builder()
                    .data("total",pageTeacher.getTotal())
                    .data("data",pageTeacher.getRecords())
                    .successBuild();
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("分页查询讲师失败")
                    .params(ArrayUtil.toString(new Long[]{current,limit}))
                    .build();
        }

    }

    @ApiOperation(value = "条件分页查询讲师")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public DataResult pageTeacherCondition(@ApiParam(name = "current",value = "当前页码数",required = true)@PathVariable Long current,
                                           @ApiParam(name = "limit",value = "每页显示条数",required = true)@PathVariable Long limit,
                                           @RequestBody(required = false) TeacherQueryVo teacherQuery){

        try {
            String name = teacherQuery.getName();
            Integer level = teacherQuery.getLevel();
            String begin = teacherQuery.getBegin();
            String end = teacherQuery.getEnd();

            QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
            if (StrUtil.isNotEmpty(name)){
                wrapper.like("name",name);
            }
            if (null!=level){
                wrapper.eq("level",level);
            }
            if (StrUtil.isNotEmpty(begin)){
                wrapper.ge("gmt_create",begin);
            }
            if (StrUtil.isNotEmpty(end)){
                wrapper.le("gmt_create",end);
            }
            wrapper.orderByDesc("gmt_create");
            //创建分页对象
            //调用方法实现，底层将查询出的数据封装到pageTeacher对象中
            Page<EduTeacher> pageTeacher = eduTeacherService.page(new Page<>(current, limit), wrapper);

            return DataResult.builder()
                    .data("total",pageTeacher.getTotal())
                    .data("data",pageTeacher.getRecords())
                    .successBuild();
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("条件分页查询讲师失败")
                    .params(ArrayUtil.toString(new Object[]{current,limit,teacherQuery}))
                    .build();
        }
    }

    @ApiOperation(value = "添加讲师")
    @PostMapping("addTeacher")
    public DataResult addTeacher(@RequestBody EduTeacher eduTeacher){

        try {
            return DataResult.builder().data(eduTeacherService.save(eduTeacher)).successBuild();
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("添加讲师失败")
                    .params(eduTeacher)
                    .build();
        }
    }

    @ApiOperation(value = "根据id查询讲师")
    @GetMapping("getTeacherById/{id}")
    public DataResult getTeacherById(@PathVariable String id){
        try {
            return DataResult.builder().data(eduTeacherService.getById(id)).successBuild();
        }catch (Exception e){
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("根据id查询讲师失败")
                    .params(id)
                    .build();
        }

    }

    @ApiOperation(value = "根据id修改讲师")
    @PostMapping("updateTeacherById")
    public DataResult updateTeacherById(@RequestBody EduTeacher eduTeacher){
        try {
            return DataResult.builder().data(eduTeacherService.updateById(eduTeacher)).successBuild();
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("根据id修改讲师失败")
                    .params(eduTeacher)
                    .build();
        }
    }
}

